package com.stk.triplecity;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.Paint;

public class Element {
	private int posX;
	private int posY;
	private int level;
	private int type;
	private int alpha = 255;
	private boolean marked = false;
	private ArrayList<Bitmap> bitmap = null;
	private int[] endPoint = new int[2];
	private boolean moved;
	private int steps;
	private int countSteps;
	private int countImages;
	private int countDelay;
	private int tileSize;
	private ArrayList<int[]> moves;
	private boolean animation;
	private int border;


	public Element() {
		super();
	}


	public Element(int posX, int posY, int level, int type, Panel p) {
		super();
		this.posX = posX;
		this.posY = posY;
		this.level = level;
		this.type = type;
		this.moved = false;
		this.setBitmap(this.level, this.type, p);
		this.tileSize = p.getTileSize();
		this.steps = tileSize/4;
		this.countSteps  = 0;
		this.countImages  = 0;
		this.countDelay  = 0;
		this.animation = false;
		this.moves = new ArrayList<int[]>();
		this.border = p.getBorder();
	}


	public int getPosX() {
		return posX;
	}


	public void setPosX(int posX) {
		this.posX = posX;
	}


	public int getPosY() {
		return posY;
	}


	public void setPosY(int posY) {
		this.posY = posY;
	}


	public int getLevel() {
		return level;
	}


	public void setLevel(int level) {
		this.level = level;
	}


	public boolean isMarked() {
		return marked;
	}


	public void setMarked(boolean marked) {
		this.marked = marked;
	}
	
	public void increaseLevel(Panel p) {
		if (this.getType()==1 && this.getLevel()==p.getImages().getLevels()[0]){
			this.setLevel(1);
			this.setType(4);
		}else if (this.getType()==2 && this.getLevel()==p.getImages().getLevels()[1]){
			this.setLevel(1);
			this.setType(4);
		}else if (this.getType()==3 && this.getLevel()==p.getImages().getLevels()[2]){
			this.setLevel(1);
			this.setType(4);
		}else{
			this.level++;
		}
		this.setBitmap( this.level,this.getType(), p);
	}


	public int getAlpha() {
		return alpha;
	}


	public void setAlpha(int alpha) {
		this.alpha = alpha;
	}
	
	public Paint getPaint(){
		Paint paint = new Paint();
		paint.setARGB(alpha, 255, 255, 255);
		return paint;
	}

	public void decreaseAlpha(){
		this.alpha-=10;
		if (this.alpha < 0){
			this.alpha = 0;
		}
	}
	
	public void increaseAlpha(){
		this.alpha+=10;
		if (this.alpha > 255){
			this.alpha = 255;
		}
	}

	public Bitmap getBitmap(){
		if (animation || (this.getLevel()==2 && this.getType()==3)){
			countDelay++;
			if (countDelay >= 5){
				countDelay = 0;
				countImages++;
				if (countImages == bitmap.size()){
					countImages = 0;
					animation = false;
				}
			}

			return bitmap.get(countImages);
		}else{
			return bitmap.get(0);
		}
			
	}
	
	public void setBitmap(int level, int type, Panel p){
		this.bitmap = p.getImages().getImage(level, type);
		countImages = 0;
	}
		

	@Override
	public String toString() {
		return "Element [posX=" + posX + ", posY=" + posY + ", level=" + level	+ "]";
	}


	public int[] getEndPoint() {
		return endPoint;
	}


	public void setEndPoint(int[] endPoint) {
		this.endPoint = endPoint;
	}


	public int getType() {
		return type;
	}


	public void setType(int type) {
		this.type = type;
	}
	
	public boolean isMoved() {
		return moved;
	}


	public void setMoved(boolean moved) {
		this.moved = moved;
	}
	
	public void addMove(int[] i){
		moves.add(i);
	}
	
	public void move(){
		if (moves.size()>0){
			switch (moves.get(0)[0]){
			case 1: moveUp();
			break;
			case 2: moveRight();
			break;
			case 3: moveDown();
			break;
			case 4: moveLeft();
			break;
			}
		}
	}
	
	public void moveUp(){
		if (countSteps <= steps){
			this.setPosY(this.getPosY() - (tileSize/steps));
			countSteps++;
		}else{
			countSteps =0;
			this.setPosY(moves.get(0)[2]*tileSize+border);
			moves.remove(0);			
		}		
	}
	
	public void moveDown(){
		if (countSteps <= steps){
			this.setPosY(this.getPosY() + (tileSize/steps));
			countSteps++;
		}else{
			countSteps =0;
			this.setPosY(moves.get(0)[2]*tileSize+border);
			moves.remove(0);
		}
	}
	
	public void moveLeft(){
		if (countSteps <= steps){
			this.setPosX(this.getPosX() - (tileSize/steps));			
			countSteps++;
		}else{
			countSteps =0;
			this.setPosX(moves.get(0)[1]*tileSize+border);
			moves.remove(0);
		}
	}
	
	public void moveRight(){
		if (countSteps <= steps){
			this.setPosX(this.getPosX() + (tileSize/steps));
			countSteps++;
		}else{
			countSteps =0;
			this.setPosX(moves.get(0)[1]*tileSize+border);
			moves.remove(0);
		}
	}


	public void setAnimation(boolean animation) {
		this.animation = animation;
	}
	
	
}
